package vue.panels;

import java.awt.event.ActionEvent;

import javax.swing.*;

import vue.Ui;
import controler.Joueur;
import controler.Protocol;
import controler.ProtocolException;
import controler.Table;
import vue.components.*;
/**
 * <p>Title: vue.jeuPanel </p>
 *
 * <p>
 * 	Description: Cette classe gere l'affichage du panneau de jeu
 * </p>
 *
 * <p>Copyright: (c) 2008 FSSIM</p>
 *
 * <p>Company: FSSImock</p>
 *
 * @author Antoine Farnault
 * @author Augustin Couette
 * @version 1.0
 */
public class JeuPanel extends TPPanel {

	private static final long serialVersionUID = 1L;

	// Variables declaration
	private CamembertPanel camembertPanel;
	public TPLabel desLabel;
	private JScrollPane jScrollPane3;
	public TPButton lancerButton;
	private TPButton questionButton;
	public Plateau plateauCanvas;
	private TPLabel proprietaireLabel;
	private TPLabel qrLabel;
	public JTextPane qrText;
	private TPLabel tableLabel;
	private TPLabel themeLabel;
	private TPLabel tourLabel;

	public JProgressBar progressBar;
	public JButton rAButton;
	public JButton rBButton;
	public JButton rCButton;
	public JButton rDButton;

	public boolean repondu =false;
	public Ui ui;
	public Table table;
	// End of variables declaration


	/** Creates new form JeuPanel */
	public JeuPanel(Ui ui, Table table) {
		this.ui = ui;
		this.table = table;
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 */
	private void initComponents() {
		setName("jeuPanel");
		plateauCanvas = new Plateau(table);
		lancerButton = new TPButton();
		questionButton = new TPButton();
		desLabel = new TPLabel();
		jScrollPane3 = new JScrollPane();
		qrText = new JTextPane();
		qrLabel = new TPLabel();
		camembertPanel = new CamembertPanel(table);
		tableLabel = new TPLabel();
		themeLabel = new TPLabel();
		tourLabel = new TPLabel();
		proprietaireLabel = new TPLabel();
		qrText.setOpaque(false);
		
		questionButton.setText("Quitter");
		lancerButton.setEnabled(false);
		lancerButton.setIcon(new ImageIcon("desbleu.jpg"));
		lancerButton.setText("Des");
		lancerButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				lancerButtonActionPerformed(evt);
			}
		});

		lancerButton.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				lancerButtonKeyPressed(evt);
			}
		});

		desLabel.setText("Tirer le de");

		qrText.setBackground(new java.awt.Color(255, 255, 255));
		qrText.setBorder(null);
		qrText.setEditable(false);
		jScrollPane3.setViewportView(qrText);

		qrLabel.setText("Question/Réponse");

		tableLabel.setText("Table : "+table.nom);

		themeLabel.setText("Theme : "+table.theme);

		tourLabel.setText("Tour de : ");

		proprietaireLabel.setText("Propriétaire : "+table.proprietaire);

		rAButton = new javax.swing.JButton();
		rBButton = new javax.swing.JButton();
		rCButton = new javax.swing.JButton();
		rDButton = new javax.swing.JButton();
		progressBar = new javax.swing.JProgressBar();

		setBackground(new java.awt.Color(255, 255, 255));

		qrText.setBackground(new java.awt.Color(255, 255, 255));
		qrText.setBorder(null);
		qrText.setEditable(false);
		jScrollPane3.setViewportView(qrText);

		qrLabel.setText("Question/Réponse");

		tableLabel.setText("Table :");

		themeLabel.setText("Theme :");

		tourLabel.setText("Tour de");

		proprietaireLabel.setText("Propriétaire");

		rAButton.setText("A");

		rBButton.setText("B");

		rCButton.setText("C");

		rDButton.setText("D");

		rAButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				reponseActionPerformed(evt);
			}
		});
		
		rBButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				reponseActionPerformed(evt);
			}
		});
		rCButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				reponseActionPerformed(evt);
			}
		});
		rDButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				reponseActionPerformed(evt);
			}
		});
		
		rAButton.setEnabled(false);
		rBButton.setEnabled(false);
		rCButton.setEnabled(false);
		rDButton.setEnabled(false);
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tableLabel)
                    .addComponent(tourLabel)
                    .addComponent(themeLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camembertPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(progressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(qrLabel, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(rAButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(rBButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(rCButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(rDButton))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                    .addComponent(proprietaireLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lancerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(questionButton)
                        .addGap(70, 70, 70)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(plateauCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tableLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(qrLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rAButton)
                    .addComponent(rBButton)
                    .addComponent(rCButton)
                    .addComponent(rDButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(camembertPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tourLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(themeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(proprietaireLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lancerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(questionButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(2, 2, 2))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(plateauCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE))
        );
    }

	/**
	 * Action quand une action sur le bouton "Des"
	 * @param evt
	 */
	private void lancerButtonActionPerformed(java.awt.event.ActionEvent evt) {
		if(!lancerButton.isEnabled()){
			Joueur.ui.statutPanel.setInfo("Attendez votre tour !");
			return;
		}
		
		repondu = false;
		try {
			Protocol.ask_lancerDe(Joueur.name,table.nom);
		} catch (ProtocolException e1) {
			e1.printStackTrace();
			return;
		}
		lancerButton.setEnabled(false);
		
		//Thread anonyme de traitement de la progressBar
		Thread t = new Thread() {
			public void run() {
				progressBar.setValue(0);
				while (progressBar.getValue()<100) {
					try {
						sleep(200); //20 secondes
					} catch(Exception e){ 
						System.err.println("Dans le thread -------------"+e);
					}
					progressBar.setValue(progressBar.getValue()+1);
				}
				
				//Arret naturel ou non
				if (!repondu){
					try {
						Protocol.snd_reponse("timeout", table.nom, Joueur.caseRepondue.isCamembert+"");
						repondu = true;
					} catch (ProtocolException e) {
						e.printStackTrace();
					}
				}
				
				rAButton.setEnabled(false);
				rBButton.setEnabled(false);
				rCButton.setEnabled(false);
				rDButton.setEnabled(false);
				qrText.setOpaque(false);
			}
		};
		t.start();//Lancement du thread
		
	}
	
	/**
	 * Action quand une touche est presse sur le bouton "Des" 
	 * @param evt
	 */
	private void lancerButtonKeyPressed(java.awt.event.KeyEvent evt) {
		if(evt.getKeyCode()==10){ // touche entree
			lancerButtonActionPerformed(null);
		}
	}
	

	private void reponseActionPerformed(ActionEvent evt) {
		String reponse ="";
		if (!rAButton.isEnabled()){
			return;
		}
		System.out.print(""+evt.getSource());
		if (evt.getSource().equals(rAButton)){
			reponse = "rA";
		}
		if (evt.getSource().equals(rBButton)){
			reponse = "rB";
		}
		if (evt.getSource().equals(rCButton)){
			reponse = "rC";
		}
		if (evt.getSource().equals(rDButton)){
			reponse = "rD";
		}
		try {
			progressBar.setValue(100);
			repondu = true;
			Protocol.snd_reponse(reponse, table.nom, Joueur.caseRepondue.isCamembert+"");
		} catch (ProtocolException e) {
			e.printStackTrace();
		}
	}
	
}
